package com.yiqi.news.mvp.ui.activity

import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import butterknife.OnClick
import com.jess.arms.base.BaseActivity
import com.jess.arms.di.component.AppComponent
import com.jess.arms.utils.ArmsUtils
import com.jess.arms.widget.DividerItemDecoration
import com.novel.cn.ext.dp2px
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import com.yiqi.news.R
import com.yiqi.news.app.widget.qmui.QMUITipDialog
import com.yiqi.news.di.component.DaggerWithdrawComponent
import com.yiqi.news.di.module.WithdrawModule
import com.yiqi.news.entity.GoldInfo
import com.yiqi.news.entity.GoldRecord
import com.yiqi.news.mvp.contract.WithdrawContract
import com.yiqi.news.mvp.presenter.WithdrawPresenter
import com.yiqi.news.mvp.ui.adapter.CoinsAdapter
import kotlinx.android.synthetic.main.activity_withdraw.*


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 06/10/2019 17:02
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
/**
 * 如果没presenter
 * 你可以这样写
 *
 * @ActivityScope(請注意命名空間) class NullObjectPresenterByActivity
 * @Inject constructor() : IPresenter {
 * override fun onStart() {
 * }
 *
 * override fun onDestroy() {
 * }
 * }
 */
class WithdrawActivity : BaseActivity<WithdrawPresenter>(), WithdrawContract.View, OnRefreshLoadMoreListener {

    private lateinit var mData: GoldInfo
    private lateinit var mAdapter: CoinsAdapter

    override fun setupActivityComponent(appComponent: AppComponent) {
        DaggerWithdrawComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .withdrawModule(WithdrawModule(this))
                .build()
                .inject(this)
    }


    override fun initView(savedInstanceState: Bundle?): Int {
        return R.layout.activity_withdraw //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }


    override fun onStart() {
        super.onStart()
        mPresenter?.requestGoldInfo()
        mPresenter?.requestGoldRecord(true)
    }

    override fun initData(savedInstanceState: Bundle?) {
        initAdapter()
        initListener()
    }

    private fun initListener() {
        refresh_layout.setOnRefreshLoadMoreListener(this)
    }

    private fun initAdapter() {

        rv_coins_record.layoutManager = LinearLayoutManager(this)
        rv_coins_record.addItemDecoration(DividerItemDecoration(DividerItemDecoration.VERTICAL).setHeight(dp2px(4)))
        mAdapter = CoinsAdapter()
        mAdapter.bindToRecyclerView(rv_coins_record)


    }

    override fun showLoading() {

    }

    override fun hideLoading() {

    }

    override fun showMessage(message: String) {
        ArmsUtils.snackbarText(message)
    }

    override fun launchActivity(intent: Intent) {
        ArmsUtils.startActivity(intent)
    }

    override fun killMyself() {
        finish()
    }

    override fun setData(data: GoldInfo) {
        mData = data
        tv_now_money.text = data.run { goldNow.toString() }
        tv_today_money.text = data.run { goldToday.toString() }
        tv_total_money.text = data.run { goldAdd.toString() }
    }

    override fun hasMore(hasMore: Boolean) {
        refresh_layout.setNoMoreData(!hasMore!!)
    }

    override fun complete(isRefresh: Boolean) {
        if (isRefresh!!) {
            refresh_layout.finishRefresh()
        } else {
            refresh_layout.finishLoadMore()
        }
    }

    override fun setGoldRecordData(data: ArrayList<GoldRecord>, isRefresh: Boolean) {
        if (isRefresh) {
            mAdapter.setNewData(data)
        } else {
            mAdapter.addData(data)
        }
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        mPresenter?.requestGoldRecord(false)
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        mPresenter?.requestGoldRecord(true)
    }

    override fun useImmersionBar(): Boolean {
        return true
    }

    override fun initImmersionBar() {
        super.initImmersionBar()
        mImmersionBar.titleBar(R.id.tool_withdraw).init()
    }

    @OnClick(R.id.btn_user_withdraw, R.id.iv_back)
    fun onClick(view: View) {
        var bundle = Bundle()
        bundle.putInt("gold_now",mData.goldNow)
        when (view.id) {
            R.id.btn_user_withdraw -> if (mData != null && mData.goldNow > 1000) ArmsUtils.startActivity(this,ExchangeActivity::class.java,bundle) else mQMUIInfoDialog.show()
            R.id.iv_back -> finish()
        }
    }

    private val mQMUIInfoDialog: QMUITipDialog by lazy {
        return@lazy QMUITipDialog.Builder(this)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_FAIL)
                .setTipWord("金币不足")
                .create()
    }
}
